package com.surelution.projects.crm
import groovy.xml.MarkupBuilder

class LocationController {
	
	def gasPriceService

    def index = { 
		def cat = PageEntry.get(params.id)
		def locateList = cat.children.findAll({it.showInMenu}).sort {it.positionSeq}
		[locateList:locateList,location:cat]
	}
	
	def oStationList = {
		def os
		def cSize,mSize
		Date today = new Date()
		if(params.oId) {
			os = OwnStation.get(params.oId)
		} else
			os = OwnStation.list([cache:true])
		render(contentType:"text/xml") {
			stations(){
				os.each{s->
					def stationGasList = StationGas.findAllByStation(s)
					def pic = 0
					def pictures = s.pictures
					pictures.each {
						if(it&&pic==0) {
							pic = it.id
						}
					}
					if(s.id) {
						cSize = OwnStation.executeQuery("select count(os.id) from CompetitorStation os where os.station.id=?", [s.id])
						mSize = OwnStation.executeQuery("select count(os.id) from Market os where os.station.id=?",[s.id])
//						def station = OwnStation.get(s.id)
//						if(CompetitorStation.countByStation(station)){
//							cSize = CompetitorStation.countByStation(station)
//						}
//						else cSize = 0
//						if(Market.findAllByStation(station)){
//							mSize = Market.countByStation(station)
//						}
//						else mSize = 0
					} 
					station("id":s.id, name:s.shortName, 
						latitude:s.latitude, longitude:s.longitude, 
						address:s.address,pic:pic,cSize:cSize,mSize:mSize,annualSale:s.annualSale) {
						gasOnSales() {
							stationGasList.each {sg->
								def price = gasPriceService.getStationGasPrice(s, sg.gasType)
								def isPromoting = false
								def stationGas = StationGas.findByStationAndGasType(s, sg.gasType)
								if(stationGas) {
									def cri = Promoting.createCriteria()
									def promoting = cri.list() {
										eq("stationGas", stationGas)
										le("startDate", today)
										or{
											ge("endDate", today)
											isNull("endDate")
										}
									}
									if (promoting){
										isPromoting = true
										}
								}
								gasOnSale("id":sg.id, "gasType":sg.gasType.shortName, "price":price, "promoting":isPromoting)
							}
						}
					}
				}
			}
		}
	}
	
	def gStationList = {
		def cs,size
		Date today = new Date()
		if(params.oId) {
			def station = OwnStation.get(params.oId)
			cs = CompetitorStation.findAllByStation(station)
		} else
			cs = CompetitorStation.list()
		render(contentType:"text/xml") {
			gstations(){			
				cs.each{s->
					def stationGasList = StationGas.findAllByStation(s)
					def pic = 0
					def pictures = s.pictures
					pictures.each {
						if(it&&pic==0) {
							pic = it.id
						}
					}
					cstation("id":s.id, name:s.name,company:s.company.name,
						latitude:s.latitude, longitude:s.longitude,
						address:s.address,iconId:s.company.id,pic:pic,annualSale:s.annualSale) {
						gasOnSales() {
							stationGasList.each {sg->
								def price = gasPriceService.getStationGasPrice(s, sg.gasType)
								def isPromoting = false
								def stationGas = StationGas.findByStationAndGasType(s, sg.gasType)
								if(stationGas) {
									def cri = Promoting.createCriteria()
									def promoting = cri.list() {
										eq("stationGas", stationGas)
										le("startDate", today)
										or{
											ge("endDate", today)
											isNull("endDate")
										}
									}
									if (promoting){
										isPromoting = true
										}
								}
								gasOnSale("id":sg.id, "gasType":sg.gasType.name, "price":price, "promoting":isPromoting)
							}
						}
					}
				}
			}
		
		}
	}

	def marketList = {
		def ms
		if(params.oId) {
			def station = OwnStation.get(params.oId)
			ms = Market.findAllByStation(station)
			println ms
		} else
			ms = Market.list()
		render(contentType:"text/xml") {
			markets(){
				ms.each{s->
					def pic = 0
					def pictures = s.pictures
					pictures.each {
						if(it&&pic==0) {
							pic = it.id
						}
					}
					
					market("id":s.id, name:s.name,latitude:s.latitude, longitude:s.longitude,address:s.address,pic:pic,annualSaleNumber:s.annualSaleNumber,type:s.marketType.id) {
					}
				}
			}
		}
	}	
	
}
